package com.guigu.tms.aggregation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.tms.aggregation.mapper.OrderLocationMapper;
import com.guigu.tms.aggregation.mapper.OrderMapper;
import com.guigu.tms.aggregation.service.IOrderCargoService;
import com.guigu.tms.aggregation.service.ITaskPickupDispatchService;
import com.guigu.tms.commons.aggregation.entity.Order;
import com.guigu.tms.commons.aggregation.entity.TransportOrder;
import com.guigu.tms.aggregation.mapper.TransportOrderMapper;
import com.guigu.tms.aggregation.service.ITransportOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.tms.commons.aggregation.entity.TransportOrderQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 运单表 服务实现类
 * </p>
 *
 * @author wj
 * @since 2022-10-21
 */
@Service
public class TransportOrderServiceImpl extends ServiceImpl<TransportOrderMapper, TransportOrder> implements ITransportOrderService {

    @Autowired
    private TransportOrderMapper transportOrderMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IOrderCargoService cargoService;
    @Autowired
    private OrderLocationMapper orderLocationMapper;
    @Autowired
    private ITaskPickupDispatchService pickupDispatchService;


    @Override
    public PageInfo<TransportOrder> getTransportOrders(TransportOrderQuery transportOrderQuery, Integer num, Integer size) {
        PageHelper.startPage(num==null?1:num,size==null?5:size);
        List<TransportOrder> transportOrders = transportOrderMapper.getTransportOrders(transportOrderQuery);
        for (TransportOrder transportOrder : transportOrders) {
            //查询订单
            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("order_id", transportOrder.getOrderId());
            Order order = orderMapper.selectOne(wrapper);

            //获取货物信息
            order.setOrderCargo(cargoService.getCargoByOrderId(order.getOrderId()));
            //获取订单位置信息
            order.setOrderLocation(orderLocationMapper.selectOne(wrapper));
            //获取运单信息
            order.setTransportOrder(transportOrderMapper.selectOne(wrapper));
            //获取取件信息
            order.setTaskPickup(pickupDispatchService.getTaskPickupDispatchByOrderId(order.getOrderId(),1));
            //获取派件信息
            order.setTaskDispatch(pickupDispatchService.getTaskPickupDispatchByOrderId(order.getOrderId(),2));
            transportOrder.setOrder(order);
        }
        return new PageInfo<>(transportOrders);
    }
}
